package com.jic.market.impl.rest.lottery;

import java.io.Serializable;
import java.util.*;

/**
 * 抽奖计算
 */
public class Lottery {
    protected final static Random random = new Random();
    private Integer codeScope = Integer.valueOf(1);// 抽奖随机数区间
    private Integer winningStartCode = Integer.valueOf(0);// 中奖数值区间起始值
    private Integer winningEndCode = Integer.valueOf(0);// 中奖数值区间结束值
    private HashMap awardMap = new HashMap();// 奖品Map
    private List<LotteryAward> lotteryAwardList;

    public Lottery(List<LotteryAward> lotteryAwardList) {
        this.lotteryAwardList = lotteryAwardList;
    }

    /**
     * 中奖计算逻辑
     */
    public LotteryAward lottery() {

        // 获取奖品中奖区间
        getAwardScope(lotteryAwardList);

        // 从中奖数字区间内随机选一数字
        Integer randomCode = Integer.valueOf(random.nextInt(codeScope
                .intValue()));
        // 如果随机数字在中奖区间内
        if (randomCode.intValue() >= winningStartCode.intValue()
                && randomCode.intValue() <= winningEndCode.intValue()) {
            Iterator i$ = awardMap.entrySet().iterator();
            while (i$.hasNext()) {
                Map.Entry awardRpcEntry = (Map.Entry) i$.next();
                LotteryAward awardRpcVo = (LotteryAward) awardRpcEntry
                        .getValue();
                // 如果随机数在奖品的中奖数值区间内 则此奖品为中奖奖品
                if (randomCode.intValue() >= awardRpcVo.getAwardStartCode()
                        .intValue()
                        && randomCode.intValue() <= awardRpcVo
                        .getAwardEndCode().intValue()) {
//					System.out.println("中奖：" + awardRpcEntry.getKey());
                    return awardRpcVo;
                }
            }
        }

        return null;
    }

    /**
     * 设置奖品中奖区间
     *
     * @param lotteryAwardList
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void getAwardScope(List<LotteryAward> lotteryAwardList) {
        Iterator lotteryAwardIterator = lotteryAwardList.iterator();
        codeScope = Integer.valueOf(1);
        winningStartCode = Integer.valueOf(0);// 中奖数值区间起始值
        winningEndCode = Integer.valueOf(0);// 中奖数值区间结束值
        awardMap = new HashMap();
        // 1. 遍历奖品 取到抽奖随机数区间codeScope
        while (lotteryAwardIterator.hasNext()) {
            LotteryAward lotteryAward = (LotteryAward) lotteryAwardIterator.next();
            Integer tempCodeScope = Integer.valueOf(1);
            Double awardProbability = lotteryAward.getAwardProbability();
            while (true) {
                Double vo = Double.valueOf(awardProbability.doubleValue()* (double) tempCodeScope.intValue());
                if (vo.doubleValue() >= 1.0D&& vo.doubleValue() - (double) vo.longValue() < 1.0E-4D) {
                    if (tempCodeScope.intValue() > codeScope.intValue()) {
                        codeScope = tempCodeScope;
                    }
                    break;
                }
                tempCodeScope = Integer.valueOf(tempCodeScope.intValue() * 10);

                if(awardProbability.equals(0D)){
                    break;
                }
            }
        }

        // 2. 奖品依次设置区间范围
        LotteryAward la;
        for (Iterator lotteryAwardIter = lotteryAwardList.iterator(); lotteryAwardIter.hasNext(); awardMap.put(la.getId(), la)) {
            la = (LotteryAward) lotteryAwardIter.next();
            Integer codeNum = Integer.valueOf((int) (la.getAwardProbability().doubleValue() * (double) codeScope.intValue()));
            if (codeNum.intValue() == 0) {
                la.setAwardStartCode(Integer.valueOf(codeScope.intValue() + 1));
                la.setAwardEndCode(Integer.valueOf(codeScope.intValue() + 1));
            } else {
                la.setAwardStartCode(winningEndCode);
                la.setAwardEndCode(Integer.valueOf(winningEndCode.intValue()+ codeNum.intValue() - 1));
                winningEndCode = Integer.valueOf(winningEndCode.intValue()+ codeNum.intValue());
            }
        }
        return;
    }

    /**
     * 奖品
     */
    public static class LotteryAward implements Serializable {
        private static final long serialVersionUID = 1L;
        private Long id;
        /** 中奖率 */
        private Double awardProbability;
        /** 中奖范围起始值 */
        private Integer awardStartCode;
        /** 中奖范围结束值 */
        private Integer awardEndCode;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public Double getAwardProbability() {
            return awardProbability;
        }

        public void setAwardProbability(Double awardProbability) {
            this.awardProbability = awardProbability;
        }

        public Integer getAwardStartCode() {
            return awardStartCode;
        }

        public void setAwardStartCode(Integer awardStartCode) {
            this.awardStartCode = awardStartCode;
        }

        public Integer getAwardEndCode() {
            return awardEndCode;
        }

        public void setAwardEndCode(Integer awardEndCode) {
            this.awardEndCode = awardEndCode;
        }

    }

}
